package com.arcsoft.commonbase.widgets.dialog

import android.content.Context
import android.view.Gravity
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.arcsoft.commonbase.R
import com.arcsoft.commonbase.databinding.PopToastBinding
import com.arcsoft.commonbase.ext.getLifecycleOwner
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import razerdp.basepopup.BasePopupWindow


class ToastPopup(
    context: Context,
    var mTitle: String,
    var mIcon: Int? = R.mipmap.add_success,
) : BasePopupWindow(context) {


    var mBinding: PopToastBinding? = null

    init {
        contentView = createPopupById(R.layout.pop_toast)
        popupGravity = Gravity.CENTER
        setBackPressEnable(false)
        setOutSideDismiss(false)
        isOutSideTouchable = false
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        mBinding = PopToastBinding.bind(contentView)

        mBinding?.apply {

            title.text = mTitle
            mIcon?.let { icon.setImageResource(it) }
        }

    }

    override fun showPopupWindow() {
        super.showPopupWindow()

        context.getLifecycleOwner()?.lifecycleScope?.launch {
            delay(2000)
            dismiss()
        }
    }


}

data class ToastBean(
    var mTitle: String,
    var mIcon: Int? = R.mipmap.add_success,
)